<?php

/**
 * products actions.
 *
 * @package    sf_sandbox
 * @subpackage products
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class productsActions extends sfActions 
{ 
  public function executeIndex() 
  { 
    $this->productsList = ProductsPeer::doSelect(new Criteria()); 
  }  
  
  public function executeCreate() 
  { 
    $this->form = new ProductsForm();  
    $this->setTemplate('edit'); 
  }  
  
  public function executeEdit($request) 
  { 
    $this->form = new ProductsForm(ProductsPeer::retrieveByPk($request->getParameter('products_id'))); 
  }  
  
  public function executeUpdate($request) 
  { 
    $this->forward404Unless($request->isMethod('post'));  
    $this->form = new ProductsForm(ProductsPeer::retrieveByPk($request->getParameter('products_id')));  
    $this->form->bind($request->getParameter('products'), $request->getFiles('products'));  
    if ($this->form->isValid()) 
    { 
      $this->form->save(); 
      $products = $this->form->getObject();  
      $file = $this->form->getValue('products_image');  
      $filename = $products->getProductsId().$file->getExtension($file->getOriginalExtension()); 
      $file->save(sfConfig::get('sf_upload_dir').'/products/originals/'.$filename); 
      $products->setProductsImage($filename); 
      $products->save();  
      $thumbnail = new sfThumbnail(150, 150); 
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/products/originals/'.$filename); 
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/products/thumbnails/'.$filename);  
      $thumbnail = new sfThumbnail(30, 30); 
      $thumbnail->loadFile(sfConfig::get('sf_upload_dir').'/products/originals/'.$filename); 
      $thumbnail->save(sfConfig::get('sf_upload_dir').'/products/cart_thumbnails/'.$filename); 
      $this->redirect('products/edit?products_id='.$products->getProductsId()); 
    }  
    $this->setTemplate('edit'); 
  }  
  
  public function executeDelete($request) 
  { 
    $this->forward404Unless($products = ProductsPeer::retrieveByPk($request->getParameter('products_id')));  
    $products->delete();  $this->redirect('products/index'); 
  } 
} 
